<?php

//Deal expires at 10am
include '../../../wp-blog-header.php';
require_once('rss_fetch.inc');

$url =  'http://www.feed43.com/8236041487638728.xml';

$brand_id = '202';
$rss = fetch_rss($url);

//echo "<pre>";
//print_r($rss->items);
//exit();



if(!empty($rss->items) && is_array($rss->items))
{
  foreach($rss->items as $key=>$value)
  {
    $fetched_values = explode('~',$value['description']);
    $final_values = explode('$',$fetched_values['1']);
     
    //echo '<pre>';
    //print_r($final_values);
    //echo '</pre>';
    //exit();
     
    $deal_link = 'http://affiliate.bindaasbargain.com/scripts/click.php?a_aid=Buzzr&amp;a_bid=67964431';
    $name = $value['title'];
    
    $columns_deal['name'] = pods_sanitize(ucwords(strtolower($name)));
    $columns_deal['camp_link'] = substr(sanitize_title($columns_deal['name']),0,128);
    
    $deal = new Pod('deal');
    $where = 't.camp_link = "'.$columns_deal['camp_link'].'" AND t.daily = "1"';
    $deal->findRecords('id DESC', 1,$where);
    $dealsFound = $deal->getTotalRows();
    //echo $dealsFound;
    if($dealsFound == 0)
    {
      unset($current_cities);
      $City = new Pod('city');
    	$City->findRecords('name ASC', 1000);
    	while($City->fetchRecord())
    	{
      	$ids = $City->get_field('id');
      	$current_cities[] = $ids;
    	}
      $current_cities = implode(',',$current_cities);
      $columns_deal['city'] = $current_cities;
      
      $shortened_url = make_bitly_url($deal_link);
      if(empty($shortened_url))
      {
        $shortened_url = make_goog_url($deal_link);
      }
      $columns_deal['extlink'] = $shortened_url;
      
      $columns_deal['camp_end'] = date('Y-m-d 10:00:00',strtotime('+1 day'));
      $columns_deal['camp_start'] = date('Y-m-d H:i:s');
      $columns_deal['brand'] = $brand_id;
      $columns_deal['daily'] = '1';
      $final_values['2'] = str_replace(',','',$final_values['2']);
      $final_values['3'] = str_replace(',','',$final_values['3']);
      $columns_deal['oriprice'] = $final_values['2'];
      $columns_deal['disprice'] = $final_values['3'];
    
      $columns_deal['disper'] = floor(((($columns_deal['oriprice'] - $columns_deal['disprice'])/$columns_deal['oriprice'])*100));
      
      $columns_deal['ext_image_url'] = $final_values['1'];
    
      $columns_deal['camp_cat'] = '25';//shopping
      
      $api = new PodAPI();
      $params = array('datatype' => 'deal', 'columns' => $columns_deal);
    
      //echo '<pre>';
      //print_r($params);
      //echo '</pre>';
      //exit();
     
      $api->save_pod_item($params);
    }
    
  }
}
?>
